import { $get, $post, $put, $get_ } from '@js/request'
import { AccountRoleSchema } from '@src/components/schema/account'
import { RoleShcema } from '@src/components/schema/role'

// 添加账号
export async function addAccountRequest (d: any): Promise<void> {
  const res = await $post('/api/common/account', d)
  return res.data
}

// 修改账号
export async function updateAccountRequest (d: any): Promise<void> {
  const id = d.id as string
  delete d.id
  const res = await $put(`/api/common/account/${id}`, d)
  return res.data
}

// 获取账号的角色列表
export async function getAccountRolesRequest (accountid: string): Promise<AccountRoleSchema[]> {
  const res = await $get('/api/common/role_access', { accountid })
  return res.data
}

// 获取全部角色列表
export async function getRoleListRequest (): Promise<RoleShcema[]> {
  const res = await $get_('/api/common/role')
  return res.data
}

// 更新账号角色列表
export async function updateAccountRoleRequest (roleid: string[], accountid: string): Promise<void> {
  const res = await $post('/api/common/role_access_more', {
    roleid,
    accountid
  })
  return res.data
}

// 更新默认角色
export async function updateDefaultRole (accountid: string, roleid: string): Promise<void> {
  const res = await $put('/api/common/account/set_default_role', {
    accountid,
    roleid_default: roleid
  })
  return res.data
}

// 获取职务枚举
export async function getPortEnumRequest (): Promise<string[]> {
  const res = await $get('/api/common/account/enum_post')
  return res.data.filter((li: string) => typeof li === 'string' && li !== '')
}
